package shop;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class CreateProductService implements CreateProduct {
	DBProduct  DBP;
	
	@Autowired
	public CreateProductService(DBProduct DBP){
		this.DBP = DBP;
	}
	
	@Transactional
	public boolean createProduct(Product product){
		boolean result = false;
		try{
			if(!DBP.checkProduct(product.getId())){
				result = DBP.createProduct(product);
				result = true;
			}else{
				throw new DuplicateProductException();
			}
		}catch(RuntimeException e){
			throw e;
		}
		return result;
	}
	
}
